import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 98152
 * Date: 2024-10-15
 * Time: 20:56
 */
public class Test2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int strsLen = in.nextInt();
        in.nextLine();
        String str1 = in.next();
        String str2 = in.next();
        in.nextLine();
        String[] strs = new String[strsLen];
        int str1Index = -1;
        int str2Index = -1;
        for (int i = 0; i < strsLen; i++) {
            strs[i] = in.nextLine();
            if (str1.equals(strs[i])) {
                if (str1Index == -1 || str2Index == -1 ||
                        (Math.abs(str1Index - str2Index) > Math.abs(i - str2Index)) ) {
                    str1Index = i;
                }
            }
            if (str2.equals(strs[i]))
                if (str2Index == -1 || str1Index == -1 ||
                        Math.abs(str1Index - str2Index) > Math.abs(i - str1Index)) {
                    str2Index = i;
                }
        }
        if (str1Index == -1 || str2Index == -1) {
            System.out.println(-1);
        } else {
            int min = Math.abs(str1Index - str2Index);
            System.out.println(min);
        }
    }
}
